package com.sx.controller;

import com.github.pagehelper.PageInfo;
import com.sx.domain.PromotionAd;
import com.sx.domain.PromotionAdVo;
import com.sx.domain.ResponseResult;
import com.sx.service.PromotionAdService;
import com.sx.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {
    /**
     * 广告分页查询
     */
    @Autowired
    private PromotionAdService promotionAdService;


    @RequestMapping("/findAllPromotionAd")
    public ResponseResult findAllAdByPage(PromotionAdVo adVo) {
        PageInfo allAdByPage = promotionAdService.findPromotionAdByPage(adVo);

        ResponseResult responseResult = new ResponseResult(true, 200, "响应成功",
                allAdByPage);
        return responseResult;
    }

    /**
     * 图片上传（支持GET/POST方法）
     */
    @RequestMapping(value = "/PromotionAdUpload", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseResult fileUpload(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) {
        // 如果是GET请求，返回提示信息
        if (request.getMethod().equals("GET")) {
            HashMap<String, String> info = new HashMap<>();
            info.put("message", "请使用POST方法上传文件");
            info.put("example", "POST /course/courseUpload 通过form-data上传文件，参数名为file");
            return new ResponseResult(true, 200, "接口信息", info);
        }

        // 判断文件是否为空
        if (file == null || file.isEmpty()) {
            return new ResponseResult(false, 400, "文件为空，请选择文件上传", null);
        }

        try {
            // 使用工具类上传文件
            Map<String, String> fileInfo = FileUploadUtils.uploadFile(file, request);

            // 返回给前端响应
            return new ResponseResult(true, 200, "上传成功", fileInfo);
        } catch (IOException e) {
            return new ResponseResult(false, 500, "文件上传失败: " + e.getMessage(), null);
        } catch (Exception e) {
            return new ResponseResult(false, 500, "服务器内部错误: " + e.getMessage(), null);
        }
    }

    /**
     * 广告上下线
     * 简单的状态更新操作使用 @RequestParam
     * 复杂的对象保存/更新操作使用 @RequestBody
     */
    @RequestMapping("/updatePromotionAdStatus") // 或 @PutMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(@RequestParam Integer id, @RequestParam Integer status) {
        try {
            promotionAdService.updatePromotionAdStatus(id, status);
            return new ResponseResult(true, 200, "操作成功", null);
        } catch (Exception e) {
            return new ResponseResult(false, 500, "服务器内部错误: " + e.getMessage(), null);
        }
    }

    /**
     * 新增和修改广告
     */
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd) {
        try {
            if (promotionAd.getId() == null){
                Date date = new Date();
                promotionAd.setCreateTime(date);
                promotionAd.setUpdateTime(date);
                promotionAdService.savePromotionAd(promotionAd);
                return new ResponseResult(true,200,"新增成功",null);
            }else {
                Date date = new Date();
                promotionAd.setUpdateTime(date);
                promotionAdService.updatePromotionAd(promotionAd);
                return new ResponseResult(true,200,"修改成功",null);
            }
        }catch (Exception e){
            return new ResponseResult(false,500,"修改失败",null);
        }

    }

    /**
     * 修改回显广告信息接口
     */
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(Integer id){
        try {
            PromotionAd promotionAd = promotionAdService.updatePromotionAdById(id);
            ResponseResult result = new ResponseResult(true,200,"修改回显成功",promotionAd);
            return result;
        }catch (Exception e){
            return new ResponseResult(false,500,"响应成功: " + e.getMessage(),null);
        }


    }


}